package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modelo.mMensajes;
import modelo.mPlanNegocio;
import modelo.mSabio;
import negocio.nPlanNegocio;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;

@Controller
public class PlanNegocioController {
	
	@RequestMapping(value = "/planes")
	protected ModelAndView terminar(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String pagina = "";
		String accion = request.getParameter("ind");
		ModelAndView ret = null;
		if(accion.equals("R")){
			// listado de planes de negocio
			
			nPlanNegocio oPla = new nPlanNegocio();
			pagina = "proyRegPlanNegocio";
			ret = new ModelAndView(pagina,"CATEGORIAS",oPla.lsCategoria());
		}else if(accion.equals("L")){
			// ir a menu de sabio
			mSabio mSa = new mSabio();
			HttpSession sessionActual = request.getSession(true);
			sessionActual.isNew();
			mSa = (mSabio)sessionActual.getAttribute("Usuario");
			nPlanNegocio oPlan = new nPlanNegocio();
			pagina = "proyMdi";
			System.out.print(mSa.getIdSabio());
			ret = new ModelAndView(pagina,"PLANES",oPlan.listar(mSa.getIdSabio()));
			//ret = new ModelAndView(pagina);
		}
		return ret;
	}
	
	@RequestMapping(value = "/regPlan")
	protected ModelAndView grabaPlan(@RequestParam("foto") MultipartFile file, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String pagina = "proyMenPlan";
		mPlanNegocio mPla = new mPlanNegocio();
		
		HttpSession sessionActual = request.getSession(true);
		sessionActual.isNew();
		mSabio mAd = (mSabio) sessionActual.getAttribute("Usuario");
		
		mPla.setIdSabio(mAd.getIdSabio());
		mPla.setTitulo(request.getParameter("txTitulo"));
		mPla.setIdCategoria(Integer.parseInt(request.getParameter("cbCategoria")));
		mPla.setVision(request.getParameter("txVision"));
		mPla.setRCBP(request.getParameter("txRCBP"));
		mPla.setTiempoEjecucion(request.getParameter("txEjec"));
		mPla.setInversion(Float.parseFloat(request.getParameter("txInversion")));
		mPla.setMargen(Float.parseFloat(request.getParameter("txMargen")));
		mPla.setRoi(request.getParameter("txROI"));
		
		nPlanNegocio nPla = new nPlanNegocio();
		String rpta = nPla.insertar(mPla);
		mMensajes objM = new mMensajes();
		objM.setTexto(rpta);
		if(rpta.equals("GRABADO CORRECTAMENTE")){
			//objM.setPagina("1");
			
			String orgFileName = file.getOriginalFilename();
			
			String filePath = "c:/documentos/" + orgFileName;
			//ModelMap modelMap= new ModelMap();
			
			File dest= new File(filePath);
			try{
				System.out.println("Copiando");
				file.transferTo(dest);
			}catch(IllegalStateException e){
				e.printStackTrace();
				//modelMap.addAttribute("result","Carga fallida: " + orgFileName);
				System.out.println("Carga fallida: " + orgFileName);
			}catch(IOException e){
				e.printStackTrace();
				//modelMap.addAttribute("result","Carga fallida: " + orgFileName);
				System.out.println("Carga fallida: " + orgFileName);
			}
			
			objM.setError(0);
		}else{
			//objM.setPagina("R");
			objM.setError(1);
		}
		System.out.println(objM.getPagina());
		return new ModelAndView(pagina,"MENSAJES",objM);
	}
}
